<?php
$taskName = $_POST['TaskName'] ?? null;
$taskContent = $_POST['TaskContent'] ?? null;
//任务名称不能为空
if (empty($taskName)){
    echo "任务名称不能为空<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}
//任务名称4-50个字
if (!(mb_strlen($taskName) >= 4 && mb_strlen($taskName) <= 50)){
    echo "任务名称4-50个字<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}
//任务内容20-2000个字
if (!(mb_strlen($taskContent) >= 20 && mb_strlen($taskContent) <= 2000)){
    echo "任务内容20-2000个字<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}

$dsn = "sqlsrv:Server=localhost;Database=Task";
$db = new PDO($dsn, "sa", "123456");

$selectSql = "select * from Task where TaskName='{$taskName}'";
$statement = $db ->query($selectSql);
$taskInfo = $statement ->fetch(PDO::FETCH_ASSOC);
if ($taskInfo){
    echo "任务已经存在了<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}

$insertSql = "insert into Task(TaskName, TaskContent) values('{$taskName}', '{$taskContent}')";
$result = $db ->exec($insertSql);
if ($result){
    echo "增加任务成功"."<br/>";
    echo "<a href='task_list.php'>返回任务列表</a>";
    exit();
} else {
    echo "增加任务失败<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}